package com.gitee.qdbp.base.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.web.context.ContextLoader;
import com.gitee.qdbp.able.beans.KeyString;
import com.gitee.qdbp.able.result.IResultMessage;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 枚举工具类
 *
 * @author zhaohuihua
 * @version 150930
 */
public abstract class EnumTools {

    private static final Pattern SEPARATOR = Pattern.compile(",");

    /**
     * 解析枚举字符串
     *
     * <pre>
     * EnumTools.parse("MALE,FEMALE", Gender.class);
     * --&gt; [ Gender.MALE, Gender.FEMALE ]
     * </pre>
     *
     * @param enums 枚举字符串
     * @param type 枚举类
     * @return 枚举列表
     */
    public static <T extends Enum<T>> List<T> parse(String enums, Class<T> type) {
        if (VerifyTools.isBlank(enums)) {
            return null;
        }
        String[] array = SEPARATOR.split(enums);
        List<T> list = new ArrayList<>();
        for (String i : array) {
            list.add(Enum.valueOf(type, i));
        }
        return list;
    }

    /**
     * 获取枚举对应语言的描述<br>
     *
     * <pre>
     * EnumTools.getEnumsResource(Gender.class, UserState.class);
     * --&gt; { Gender:[{key:"MALE",value:"男"},{key:"FEMALE",value:"女"}], UserState:[...] }
     * </pre>
     *
     * @param list
     * @return
     */
    @SafeVarargs
    public static Map<String, List<KeyString>> getEnumsResource(Class<? extends Enum<?>>... list) {
        Map<String, List<KeyString>> map = new HashMap<>();
        Locale locale = I18nTools.getLocale();
        ApplicationContext context = ContextLoader.getCurrentWebApplicationContext();

        for (Class<? extends Enum<?>> clazz : list) {
            map.put(clazz.getSimpleName(), getEnumResource(clazz, context, locale));
        }

        return map;
    }

    /**
     * 获取枚举对应语言的描述<br>
     *
     * <pre>
     * EnumTools.getEnumResource(Gender.class);
     * --&gt; [{key:"MALE",value:"男"},{key:"FEMALE",value:"女"}]
     * </pre>
     *
     * @param list
     * @return
     */
    public static List<KeyString> getEnumResource(Class<? extends Enum<?>> clazz) {
        Locale locale = I18nTools.getLocale();
        MessageSource context = ContextLoader.getCurrentWebApplicationContext();
        return getEnumResource(clazz, context, locale);
    }

    private static List<KeyString> getEnumResource(Class<? extends Enum<?>> clazz, MessageSource context,
            Locale locale) {

        List<KeyString> list = new ArrayList<>();
        Enum<?>[] enums = clazz.getEnumConstants();
        for (Enum<?> e : enums) {
            if (!"NULL".equals(e.name().toUpperCase())) {
                String key = clazz.getSimpleName() + "." + e.name();
                String txt = context.getMessage(key, null, locale);
                list.add(new KeyString(e.name(), txt));
            }
        }
        return list;
    }

    /**
     * 获取结果返回码对应语言的描述
     * 
     * @param result 结果返回码
     * @return
     */
    public static String getResultResource(IResultMessage result) {
        Locale locale = I18nTools.getLocale();
        MessageSource context = ContextLoader.getCurrentWebApplicationContext();
        return context.getMessage(result.getCode(), null, locale);
    }

    /**
     * 获取结果返回码对应语言的描述
     * 
     * @param results 结果返回码
     * @return
     */
    public static List<KeyString> getResultResources(IResultMessage... results) {
        Locale locale = I18nTools.getLocale();
        MessageSource context = ContextLoader.getCurrentWebApplicationContext();
        List<KeyString> list = new ArrayList<>();
        for (IResultMessage e : results) {
            String key = e.getCode();
            String txt = context.getMessage(key, null, locale);
            list.add(new KeyString(key, txt));
        }
        return list;
    }
}
